<?php
// 后台登陆模块公共类
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller {
	public $user_id; // 用户ID
	public $user_group; // 用户所示分组
	protected $db;
	public $re_url = "";
	public function __construct() {
		parent::__construct ();
		$this->db = D ( 'User' );
		if (! (session ( 'user_id' ) && session ( 'user_id' ) != "" && session ( 'user_id' ) != null)) {
			if (cookie ( 'user_info' ) && cookie ( 'user_info' ) != "" && cookie ( 'user_info' ) != null) {
				
				$user_info = decrypt ( cookie ( 'user_info' ), C ( 'ADMIN_KEY' ) );
				$user_info = explode ( "=", $user_info );
				$data = $this->db->where ( array (
						'user_id' => $user_info [0] 
				) )->find ();
				if (count ( $data ) < 1 || $data [0] ['password'] != $user_info [1]) {
					$this->red_url ();
				} else {
					$this->user_id = $data ['user_id'];
					$password = $data ['password'];
					session ( 'user_id', $this->user_id );
					session ( 'user_show', $data ['user_show'] );
					session ( 'password', $password );
					session ( 'user_group', $data ['user_group'] );
				}
			} else {
			redirect ( "/admin/logining/index");
			}
		}
		$this->user_id = session ( "user_id" );
		$this->user_group = session ( "user_group" );
	}
	
	// 退出登录
	public function logout() {
		session ( '[destroy]' );
		cookie ( 'user_info', null );
		redirect ( "/admin/logining/index");
	}
	
	// 登录,注销,失败 重定向地址
	public function red_url() {
		redirect ( $this->re_url, 0, '页面跳转中...' );
		exit ();
	}
	
	/**
	 * 删除文章
	 * 
	 * @access public
	 * @param int $id
	 *        	文章id
	 * @param int $cat_id
	 *        	栏目id
	 */
	public function delete() {
		$id = $_POST ['id'];
		$cate_id = $_POST ['cate_id'];
		$cateinfo = $this->getCateInfo ( $cate_id );
		
		$this->db = D ( $cateinfo ['table'] );
		
		echo $this->db->delete ( $id );
		exit ();
	}
	
	/**
	 * 获取栏目信息
	 * 
	 * @access public
	 * @param int $cate_id
	 *        	栏目id
	 */
	public function getCateInfo($cate_id = "") {
		$this->db = D ( 'Cate' );
		// 获得栏目信息
		$data = $this->db->get_cate ();
		
		if ($cate_id == "") {
			return $data;
		} else {
			return $data [$cate_id];
		}
	}
	
	/**
	 * 获取分页信息
	 * 
	 * @access public
	 * @param int $count
	 *        	总条数
	 * @param int $num
	 *        	单页条数
	 */
	public function getPage($count, $num = 10) {
		$Page = new \Think\Page ( $count, $num ); // 实例化分页类 传入总记录数（这是根据@979137的意见修改的，这个建议非常好！）
		$Page->setConfig ( "first", "&laquo;第一页" );
		$Page->setConfig ( "prev", "&laquo; 上一页" );
		$Page->setConfig ( "next", "下一页&raquo;" );
		$Page->setConfig ( "last", "最后一页&raquo;" );
		$Page->setConfig ( "theme", "%HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE% %DOWN_PAGE% %END%" );
		return $Page; // 分页显示输出
	}
	
	/**
	 * 检查令牌环表单重复提交
	 * @access  public 
	 * @param src  表单提交的数据
	 * @param src $url 表单提交的数据
	 * */
	public function checkToken(&$data){
			if($this->db->autoCheckToken($data)){
				unset($data['__hash__']);
				return true;
			}else{
				$this->error("hash数据不正确!",$_SERVER['HTTP_REFERER'],5);
			}
		
	}
	
	
	
}

?>